/*
 * Copyright (c) 2019 Hitevision
 */

package tw.com.hitevision.whiteboard.android.util;

import android.util.Log;

import tw.com.hitevision.whiteboard.android.Device;
import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.Whiteboard;

public class InputManager {
    private static final int INDEX_DEVICE_FINGER = 0;
    private static final int INDEX_DEVICE_STYLUS = 1;
    private static final int INDEX_DEVICE_PALM = 2;

    private static InputManager sInstance;

    public static InputManager getInstance() {
        if (sInstance == null) {
            sInstance = new InputManager();
        }
        return sInstance;
    }

    private Whiteboard.Input priorityInput;
    private Whiteboard.Input[] deviceInputs;

    private Whiteboard.Input whiteboardPriorityInput;
    private Whiteboard.Input[] whiteboardDeviceInputs;
    private Whiteboard.Input annotationPriorityInput;
    private Whiteboard.Input[] annotationDeviceInputs;

    private InputManager() {
        whiteboardDeviceInputs = new Whiteboard.Input[3];
        annotationDeviceInputs = new Whiteboard.Input[3];

        reset();
    }

    public void reset() {
        final boolean isOem = Props.getFlavorModel().equals(Props.Flavor.Model.Oem);

        whiteboardDeviceInputs[INDEX_DEVICE_FINGER] = isOem ? Whiteboard.Input.HIGHLIGHTER : Whiteboard.Input.PEN;
        whiteboardDeviceInputs[INDEX_DEVICE_STYLUS] = Whiteboard.Input.PEN;
        whiteboardDeviceInputs[INDEX_DEVICE_PALM] = Whiteboard.Input.PARTIAL_ERASER;

        annotationDeviceInputs[INDEX_DEVICE_FINGER] = isOem ? Whiteboard.Input.HIGHLIGHTER : Whiteboard.Input.PEN;
        annotationDeviceInputs[INDEX_DEVICE_STYLUS] = Whiteboard.Input.PEN;
        annotationDeviceInputs[INDEX_DEVICE_PALM] = Whiteboard.Input.PARTIAL_ERASER;

        deviceInputs = whiteboardDeviceInputs;

        annotationPriorityInput = null;
        whiteboardPriorityInput = null;
        priorityInput = null;
    }

    public void setStateMode(boolean annotationMode) {
        if (annotationMode) {
            deviceInputs = annotationDeviceInputs;

            whiteboardPriorityInput = priorityInput;
            priorityInput = annotationPriorityInput;
        } else {
            deviceInputs = whiteboardDeviceInputs;

            annotationPriorityInput = priorityInput;
            priorityInput = whiteboardPriorityInput;
        }
    }

    public Whiteboard.Input getInput(Device device) {
        switch (device) {
            case FINGER:
                return priorityInput != null ? priorityInput : deviceInputs[INDEX_DEVICE_FINGER];
            case STYLUS:
                return priorityInput != null ? priorityInput : deviceInputs[INDEX_DEVICE_STYLUS];
            case PALM:
                return deviceInputs[INDEX_DEVICE_PALM];
            default:
                return null;
        }
    }

    public void setInput(Device device, Whiteboard.Input input) {
        Log.d("inputmanager", "setInput: QQ "+input +"device= "+device );
        if (isPriorityInput(input)) {
            priorityInput = input;
        } else {
            priorityInput = null;

            switch (device) {
                case FINGER: {
                    deviceInputs[INDEX_DEVICE_FINGER] = input;
                    break;
                }
                case STYLUS: {
                    deviceInputs[INDEX_DEVICE_STYLUS] = input;
                    break;
                }
                case PALM: {
                    deviceInputs[INDEX_DEVICE_PALM] = input;
                    break;
                }
            }
        }
    }

    private static boolean isPriorityInput(Whiteboard.Input input) {
        return input == Whiteboard.Input.AUTOSHAPE || input == Whiteboard.Input.TEXT || input == Whiteboard.Input.SELECT || input == Whiteboard.Input.MOVE_CANVAS;
    }

}
